package com.hqyj.lyf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.lyf.mapper.QuestionBankMapper;
import com.hqyj.lyf.pojo.QuestionBank;
import com.hqyj.lyf.pojo.Subject;

import com.hqyj.lyf.service.QuestionBankService;
import com.hqyj.lyf.service.SubjectService;
import com.hqyj.lyf.utils.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Qiuluoer
 *  2022/5/27 14:13
 */
@Service
public class QuestionBankServiceImpl extends ServiceImpl<QuestionBankMapper, QuestionBank> implements   QuestionBankService {
    @Autowired
    private QuestionBankMapper questionBankMapper;
    @Autowired
    private QuestionBankService questionBankService;
    @Autowired
    private SubjectService subjectService;

    Map<String, Object> map = new HashMap<>();


    @Override
    public Map<String,Object> queryAllQuestion(int page,int limit) {

        return queryByQuestion(page,limit,new QuestionBank());
    }

    @Override
    public Map<String,Object> queryByQuestion(int page,int limit,QuestionBank question) {
        map.clear();
        QueryWrapper<QuestionBank> queryWrapper = new QueryWrapper<>();
        String quContent = question.getQuContent();
        String quType = question.getQuType();
        Integer subId = question.getSubId();

        if(quContent != null && !quContent.equals("")){
            queryWrapper.like("qu_content",quContent);
            System.out.println("qu_content------------");
        }
        if (quType != null && !quType.equals("")){
            queryWrapper.eq("qu_type",quType);
            System.out.println("qu_type------------");
        }
        if (subId != null && !subId.equals(0)){
            queryWrapper.eq("sub_id",subId);
            System.out.println("subId------------");
        }
        queryWrapper.orderByDesc("up_datetime");

        Page<QuestionBank> page1 = new Page<>(page,limit); //参数就是页码、每页条数
        Page<QuestionBank> QuestionBankPage = questionBankMapper.selectPage(page1,queryWrapper);
        List<Subject> subjects = subjectService.queryAllSubject();



        for (QuestionBank questionBank:QuestionBankPage.getRecords()){
           for (Subject subject:subjects){
               if (questionBank.getSubId() == subject.getSubId()){
                   questionBank.setSubName(subject.getSubName());

               }
           }
        }


        if (QuestionBankPage.getSize()<0){
            map.put("code",-1);
            map.put("msg","查询失败");
            return map;

        }
        map.put("code",0);
        map.put("msg","查询成功");
        map.put("data",QuestionBankPage.getRecords());
        map.put("count",QuestionBankPage.getTotal()); //总条数
//        map.put("subjects",subjects);

        return map;
    }

    @Override
    public Map<String,Object> addQuestion(QuestionBank questionBank) {
        map.clear();
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
       questionBank.setUpDateTime(timestamp);
        int insert = questionBankMapper.insert(questionBank);
        if (insert < 0){
            map.put("code",-1);
            map.put("msg","插入失败");
            return map;
        }
        map.put("code",200);
        map.put("msg","插入成功");
        return map;
    }

    @Override
    public Map<String,Object> addQuestions(MultipartFile file){
        map.clear();
        List<QuestionBank> questionBanks = null;
        try {
            questionBanks = ExcelUtils.praseExcel(file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
            map.put("code","-1");
            map.put("msg","批量插入失败");
            return map;
        }
        List<Subject> subjects = subjectService.queryAllSubject();
        for (QuestionBank questionBank : questionBanks){
            questionBank.setUpDateTime(new Timestamp(System.currentTimeMillis()));
            for (Subject subject : subjects){
                if (subject.getSubName().equals(questionBank.getSubName())){
                    questionBank.setSubId(subject.getSubId());
                }
            }
        }
//        map = questionBankService.addQuestions(questionBanks);
        boolean blag = questionBankService.saveBatch(questionBanks);
        if (!blag){
            map.put("code","-1");
            map.put("msg","批量插入失败");
            return map;
        }
        map.put("code","1");
        map.put("msg","批量插入成功");
        return map;
    }

    @Override
    public Map<String,Object> updateQuestion(QuestionBank questionBank) {
        map.clear();
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        questionBank.setUpDateTime(timestamp);
        System.out.println(questionBank.toString());
        UpdateWrapper<QuestionBank> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("qu_id",questionBank.getQuId());
        updateWrapper.set("qu_content",questionBank.getQuContent());
        updateWrapper.set("qu_aoption",questionBank.getQuAoption());
        updateWrapper.set("qu_boption",questionBank.getQuBoption());
        updateWrapper.set("qu_coption",questionBank.getQuCoption());
        updateWrapper.set("qu_doption",questionBank.getQuDoption());
        updateWrapper.set("qu_type",questionBank.getQuType());
        updateWrapper.set("qu_score",questionBank.getQuScore());
        updateWrapper.set("qu_answer",questionBank.getQuAnswer());
        updateWrapper.set("sub_id",questionBank.getSubId());
        updateWrapper.set("up_datetime",questionBank.getUpDateTime());

        int update = questionBankMapper.update(null, updateWrapper);
        if (update < 0){
            map.put("code",-1);
            map.put("msg","修改失败");
            return map;
        }

        map.put("code",200);
        map.put("msg","修改成功");
        return map;

    }

    @Override
    public Map<String,Object> deleteQuestion(List<Integer> quIds) {
        map.clear();
        if (quIds .isEmpty()&&quIds.size()==0) {
            map.put("code",-1);
            map.put("msg","删除失败");
            return map;
        }
        QueryWrapper<QuestionBank> wrapper = new QueryWrapper<>();
        wrapper.in("qu_id",quIds);
        questionBankMapper.delete(wrapper);

        map.put("code",200);
        map.put("msg","删除成功");
        return map;

    }
    @Override
    public Map<String,Object> deleteByQuId(Integer quId) {
        map.clear();

        QueryWrapper<QuestionBank> wrapper = new QueryWrapper<>();
        wrapper.eq("qu_id",quId);
        questionBankMapper.delete(wrapper);

        map.put("code",200);
        map.put("msg","删除成功");
        return map;

    }
}
